package nbp.currency.shard;

import java.math.BigDecimal;
import java.util.Collection;
import java.util.Date;

import nbp.currency.data.CurrencyExchangeDAO;
import nbp.currency.data.CurrencyExchangeDAOImpl;
import nbp.currency.logic.CurrencyType;
import nbp.currency.logic.entity.CurrencyExchange;

public class CurrencyService {

	private CurrencyExchangeDAO currencyDAO;
	
	public CurrencyService() {
		currencyDAO = new CurrencyExchangeDAOImpl();
	}
	
	public void addCurrency(CurrencyExchangeDTO currencyExchangeDTO){
		CurrencyExchange currencyExchange = CurrencyFactory.createCurrencyExchange(currencyExchangeDTO);
		currencyDAO.addCurrencyExchange(currencyExchange,currencyExchangeDTO.getCurrencyType().getId());
	}

	public CurrencyExchangeDTO getCurrency(CurrencyType currencyType, Date date){
		CurrencyExchange currencyExchange = currencyDAO.getCurrencyExchange(currencyType.getId(), date);
		CurrencyExchangeDTO currencyExchangeDTO = CurrencyFactory.createCurrencyExchangeDTO(currencyExchange);
		return currencyExchangeDTO;
	}
	
	public void updateCurrencyExchange(CurrencyExchangeDTO currencyExchangeDTO){
		CurrencyExchange currencyExchange = CurrencyFactory.createCurrencyExchange(currencyExchangeDTO);
		currencyDAO.updateCurrencyExchange(currencyExchange);
	}
	
	public void deleteCurrencyExchange(CurrencyExchangeDTO currencyExchangeDTO){
		CurrencyExchange currencyExchange = CurrencyFactory.createCurrencyExchange(currencyExchangeDTO);
		currencyDAO.deleteCurrencyExchange(currencyExchange);
	}
	
	public CurrencyType getBiggestDifferenceExchange(Date dateFrom, Date dateTo){
		Long idCurrency = currencyDAO.getBiggestDifferenceExchange(dateFrom, dateTo);
		return CurrencyType.getCurrencyById(idCurrency);
	}
	
	public Collection<CurrencyExchangeDTO> getTop5Exchange(CurrencyType currencyType){
		Collection<CurrencyExchange> top5Exchange = currencyDAO.getTop5Exchange(currencyType.getId());
		Collection<CurrencyExchangeDTO> top5ExchangeDTO = CurrencyFactory.createCurrencyExchangeDTO(top5Exchange);
		return top5ExchangeDTO;
	}
	
	public BigDecimal getMinExchange(Date dateFrom, Date dateTo, CurrencyType currencyType){
		BigDecimal mixExchange = currencyDAO.getMinExchange(dateFrom, dateTo, currencyType.getId());
		return mixExchange;
	}
	
	public BigDecimal getMaxExchange(Date dateFrom, Date dateTo, CurrencyType currencyType){
		BigDecimal maxExchange = currencyDAO.getMaxExchange(dateFrom, dateTo, currencyType.getId());
		return maxExchange;
	}
	
	public void deleteCurrencyExchange(CurrencyType currencyType, Date date){
		currencyDAO.deleteCurrencyExchange(currencyType.getId(), date);
	}
	
}
